<?php
/* 广告管理
* @Author: Jerry-chen
* @Date:   2014-07-14 00:30:44
* @Last Modified time: 2014-07-23 23:10:11
* @Last Modified by: Jerry-chen<uranusbill@163.com>
* @ Copyright (c) 2014 http://www.cooldreamer.com All rightsreserved.
*/

class AdControl extends CommonControl
{
	public $model;

	/**
	 * [__auto 自动运行]
	 * @return [type] [description]
	 */
	public function __auto()
	{
		$this->model=K('Ad');
	}
	/**
	 * [index 列表页]
	 * @return [type] [description]
	 */
	function index()
	{
		// 查出所有图片广告
		$adData = $this->model->get_all();
		// p($adData);die;
		$this->assign('adData',$adData);
		$this->display();
	}
	/**
	 * [add_ad 添加广告]
	 */
	public function add_ad()
	{
		//查出商品的图片，标题，键接，等相关信息
		$goodsData = K('Goods')->get_all();
		//数据分配
		$this->assign('goodsData',$goodsData);
		// p($goodsData);die;
		if(IS_POST)
		{
			// p(Q('post.'));
			$addData = Q('post.');
			// p($addData['adpic']);
			// 判断图片应用是否存在
			if(Q('post.is_self')==1){
				if(is_array($addData['adpic']))
				{
					$adpic = current($addData['adpic']);
					$addData['adpic']=$adpic['path'];
				}else{
					$addData['adpic']='';
				}
				// 添加数据
				$result = $this->model->add_($addData);
				if(!$result)
				{
					$this->error('添加失败','add_ad');
				}
				
				
				$this->success('添加成功','add_ad');				
			}elseif(Q('post.is_self')==0)
			 {
			 	//得到之前存的ID
			 	$adlink = $addData['adlink0'];
			 	//给它加一个键接值
			 	// $adlink = "index.php/Index/Goods/index/goodsid/".$adlink;
			 	$adData = array(
			 		'adtitle'=> $addData['adtitle0'],
			 		'adpic'=>$addData['adpic0'],
			 		'adlink'=>$adlink,
			 		'is_self'=>0
			 		);
			 	// 添加数据
				$result = $this->model->add_($adData);
				if(!$result)
				{
					$this->error('添加失败','add_ad');
				}
				$this->success('添加成功','add_ad');		
				// $addData
			}
			
			
		}
		$this->display();
	}
	/**
	 * [del 删除]
	 * @return [type] [description]
	 */
	public function del()
	{
		$adid = Q('get.adid');
		$result = $this->model->del_(array('adid'=>$adid));
		if($result){
			$this->success('删除成功','index');
		}else{
			$this->error('删除失败','index');
		}
	}
	/**
	 * [edit 修改]
	 * @return [type] [description]
	 */
	public function edit()
	{
		
		if(IS_POST)
		{
			$addata = array(
					'adtitle'=> Q('post.adtitle'),
			 		'adpic'=>Q('post.adpic'),
			 		'adlink'=>Q('post.adlink'),
			 		'adsort'=>Q('post.adsort')

				);
			// p(Q('post.'));
			$result = $this->model->edit(array('adid'=>Q('post.adid')),$addata);
			// p($addata);die;
			// p($result);
			if($result)
			{
			$this->success('修改成功','index');
			}else{
			$this->error('修改失败','index');
			}
		}
		$adid = Q('get.adid');
		// 获昨当前ID的数据
		$data =current($this->model->get_all(array('adid'=>$adid))); 
		// p($data);die;

		$this->assign('data',$data);
		$this->display();
	}
	/**
	 * [ajax_goodsinfo ajax对广告用户自选的处理]
	 * @return [type] [description]
	 */
	public function ajax_goodsinfo()
	{
		if(IS_AJAX)
		{
			$goodsid = Q('post.goodsid');
			if($goodsid=='请选择要推荐的商品'){
				echo 0;die;
			}
			// p($goodsid);
			//获得商品的相关信息
			$goodsData = K('Goods')->get_one(array('goodsid'=>$goodsid));
			//获得商品详细的相关信息
			$goodsdesData =K('Goodsdes')->get_one(array('goodsid'=>$goodsid)); 
			//定义一个空数组
			$adInfo = array();
			// p($goodsData);
			$adInfo['adtitle'] = $goodsData['goodsname'];
			$adInfo['adpic']=$goodsdesData['goodsimage'] ;
			$adInfo['goodsid']=$goodsid;
			// p($goodsdesData);
			echo json_encode($adInfo);die;

		}
	}
}